/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.cax.controller;

import com.comac.ins.cax.domain.bo.CaxConvertInfoBo;
import com.comac.ins.cax.domain.vo.CaxConvertInfoVo;
import com.comac.ins.cax.service.ICaxConvertInfoService;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.core.validate.QueryGroup;
import com.comac.ins.common.idempotent.annotation.RepeatSubmit;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.web.core.BaseController;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;

/**
 * @author yk
 * @desc CAX格式转换
 * @date 2024-06-19 9:07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cax/convert")
public class CaxConvertInfoController extends BaseController {

    private final ICaxConvertInfoService caxConvertService;

    /**
     * CAX格式转换
     *
     * @param file        转换文件
     * @param convertType 转换类型
     * @param inputType   输入类型
     * @param outputType  输出类型
     * @return 转换记录
     */
    @Log(title = "新增CAX格式转换", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping("")
    public R<CaxConvertInfoVo> convert(@RequestParam("file") MultipartFile file,
                                       @RequestParam(value = "convertType") String convertType,
                                       @RequestParam(value = "inputType") String inputType,
                                       @RequestParam(value = "outputType") String outputType) {
        return R.ok(caxConvertService.convert(file, convertType, inputType, outputType));
    }

    /**
     * CAX格式转换记录详情查询
     *
     * @param id 记录ID
     * @return 记录详情
     */
    @GetMapping("/{id}")
    public R<CaxConvertInfoVo> getInfo(@NotNull(message = "ID不能为空") @PathVariable("id") Long id) {
        return R.ok(caxConvertService.queryById(id));
    }

    /**
     * 转换记录列表查询
     *
     * @param bo        查询BO
     * @param pageQuery 分页实体
     * @return TableDataInfo
     */
    @GetMapping("/list")
    public TableDataInfo<CaxConvertInfoVo> list(@Validated(QueryGroup.class) CaxConvertInfoBo bo, PageQuery pageQuery) {
        return caxConvertService.queryPageList(bo, pageQuery);
    }

    /**
     * 批量删除
     *
     * @param ids id数组
     * @return 是否成功标志
     */
    @Log(title = "删除CAX格式转换记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(caxConvertService.deleteWithValidByIds(Arrays.asList(ids)));
    }

    /**
     * 下载CAX格式转换-源文件
     *
     * @param response response
     * @param id       记录ID
     * @throws IOException 异常
     */
    @GetMapping("/download/source/{id}")
    public void downloadSource(HttpServletResponse response, @NotNull(message = "ID不能为空") @PathVariable("id") Long id) throws IOException {
        caxConvertService.downloadSource(response, id);
    }

    /**
     * 下载CAX格式转换-转换文件
     *
     * @param response response
     * @param id       记录ID
     * @throws IOException 异常
     */
    @GetMapping("/download/target/{id}")
    public void downloadTarget(HttpServletResponse response, @NotNull(message = "ID不能为空") @PathVariable("id") Long id) throws IOException {
        caxConvertService.downloadTarget(response, id);
    }
}
